<?php

defined("IN_TS") or die("Access Denied.");

switch($ts) {
	case "":
		$tbshareid		= intval($_GET["id"]);
		$tbshare	= $new['tbshare']->find("tbshare","tbshareid='$tbshareid'","*");
		if(!$tbshare) {
			tsNotice($new['tbshare']->__("item_not_exist"));
		}
		$user = aac('user')->getOneUser($tbshare['userid']);
		
		//该用户一共分享
		$ushare = $new['tbshare']->find("tbshare_user","userid='$tbshare[userid]'","*");
		
		$title = $tbshare['title'];
		
		//更新hot
		$db->query("UPDATE ".dbprefix."tbshare SET `hot`=`hot`+1 WHERE tbshareid='$tbshareid'");

		include template("show");
		break;
	case 'favor':
		$tbshareid = intval($_POST["tbshareid"]);
		$userid	   =  $TS_USER['user']['userid'];
		$username	= $TS_USER['user']['username'];

		//判断用户是否已经favor
		$isexist = $new['tbshare']->find("tbshare_favor",array("userid"=>$userid,"tbshareid"=>$tbshareid),"favorid");
		if($isexist) { //该用户已经favor过
			exit($new['tbshare']->__("favor_exist"));
		} else { //不存在
			$data = array(
				"userid" =>$userid,
				"username"=>$username,
				"addtime"=>time(),
				"tbshareid"=>$tbshareid,
			);
			$new['tbshare']->create("tbshare_favor",$data);//增加favor记录
			
			//更新用户favor数
			if($new['tbshare']->findCount("tbshare_favor","userid='$userid'")) {
				$db->query("UPDATE ".dbprefix."tbshare_user SET favorcount=favorcount+1 WHERE userid='$userid'");
			} else {
				$data = array(
					"userid"=>$userid,
					"username"=>$username,
					"favorcount"=>1
				);
				$new['tbshare']->create("tbshare_user",$data);
			}
			
			//更品商品被favor数
			$db->query("UPDATE ".dbprefix."tbshare SET favor=favor+1 WHERE tbshareid='$tbshareid'");
			exit("success");
		}	
		break;
}